package com.yonyou.iuap.shell.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.yonyou.iuap.pub.DateUtil;
import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.shell.dao.BdKnowledgeCategoryEntityDao;
import com.yonyou.iuap.shell.entity.BdCategoryRelationEntity;
import com.yonyou.iuap.shell.entity.BdKnowledgeCategoryEntity;
import com.yonyou.iuap.shell.entity.exp.BdKnowledgeCateTreePoJo;
import com.yonyou.iuap.shell.entity.exp.BdKnowledgeCategoryEntityVo;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IKnowledgeCategoryService;
import com.yonyou.iuap.shell.util.MathUtil3;

/**
 * 知识类别
 * 
 * **/
@Service("service-KnowledgeCategory")
public class KnowledgeCategoryServiceImpl implements IKnowledgeCategoryService {

	@Autowired
	@Qualifier("Dao-BdKnowledgeCategoryEntityDao")
	private BdKnowledgeCategoryEntityDao bdKnowledgeCategoryEntityDao;
	
	public ResultDto deleteByPrimaryKey(String paramString) {
		
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		}else{
			int i_del = bdKnowledgeCategoryEntityDao.deleteByPrimaryKey(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		
		return rsDto;
		
	}

	public ResultDto insert(BdKnowledgeCategoryEntity supplier) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			supplier.setPk_knowledge_category(MathUtil3.uuid32());
			int a=  bdKnowledgeCategoryEntityDao.insertSelective(supplier);
			if(a>0){
				rsDto.setStatus(0);
				rsDto.setMessage("添加成功！");
			}
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("添加失败！");
		}
		 return rsDto;
	}

	public ResultDto selectByPrimaryKey(String paramString) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			 BdKnowledgeCategoryEntity  listvo= (BdKnowledgeCategoryEntity) bdKnowledgeCategoryEntityDao.selectByPrimaryKey(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
		}
		return rsDto;
	}

	public ResultDto selectAll() {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
			List<BdKnowledgeCategoryEntity>  listvo= bdKnowledgeCategoryEntityDao.selectAll();
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
			rsDto.setNum(listvo==null?0:listvo.size());
			rsDto.setCount(listvo==null?0:listvo.size());
		
		return rsDto;
	}

	public ResultDto updateByPrimaryKey(BdKnowledgeCategoryEntity supplier) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			int i_del = bdKnowledgeCategoryEntityDao.updateByPrimaryKey(supplier);
			rsDto.setStatus(0);
			rsDto.setMessage("修改完成!");
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}
		
		return rsDto;
	}

	public ResultDto selectByCondition(String paramString) {
		// TODO 自动生成的方法存根
		
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			List<BdKnowledgeCategoryEntity>   listvo= bdKnowledgeCategoryEntityDao.selectByCondition(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
			rsDto.setNum(listvo==null?0:listvo.size());
			rsDto.setCount(listvo==null?0:listvo.size());
		}
		return rsDto;
	}

	public ResultDto countByCondition(String paramString) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			 int i_del = bdKnowledgeCategoryEntityDao.countByCondition(paramString);

			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setNum(i_del);
		}
		return rsDto;
	}

	public ResultDto deleteByCondition(String paramString) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		}else{
			int i_del = bdKnowledgeCategoryEntityDao.deleteByCondition(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		
		return rsDto;
	}

	public ResultDto insertSelective(BdKnowledgeCategoryEntity supplier) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			supplier.setPk_knowledge_category(MathUtil3.uuid32());
			int a=  bdKnowledgeCategoryEntityDao.insertSelective(supplier);
			if(a>0){
				rsDto.setStatus(0);
				rsDto.setMessage("添加成功！");
			}
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("添加失败！");
		}
		 return rsDto;
	}

	public ResultDto updateByPrimaryKeySelective(BdKnowledgeCategoryEntity supplier) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			int i_del = bdKnowledgeCategoryEntityDao.updateByPrimaryKeySelective(supplier);
			rsDto.setStatus(0);
			rsDto.setMessage("修改完成!");
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}
		return rsDto;
	}
	
	  /**
	   * 通过id获取知识类别
	   * @param knowledgeCategory  知识类别id
	   * @return
	   */
	public BdKnowledgeCategoryEntity getKnowledgeCategoryById(String knowledgeCategory) {
	
		return  (BdKnowledgeCategoryEntity) bdKnowledgeCategoryEntityDao.selectByPrimaryKey(knowledgeCategory);
	}

   /**
    * 获取所有类别
    * @return
    */
	public List<BdKnowledgeCategoryEntityVo> getAllKnowledgeCategory() {
		List<BdKnowledgeCategoryEntityVo>  listvo= bdKnowledgeCategoryEntityDao.selectAllNew();
		return listvo;
	}

	public List<BdKnowledgeCategoryEntity> getKnowledgeCategoryCondition(String conditionParam) {
		return bdKnowledgeCategoryEntityDao.selectByCondition(conditionParam);
	}

	/**
	 * 通过id删除知识类别
	 */
	public ResultDto deleteById(String knowledgeCategory) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(knowledgeCategory)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
			return rsDto;
		}else{
			int bj=bdKnowledgeCategoryEntityDao.selectById(knowledgeCategory);
			if(bj >= 1 ){
				rsDto.setStatus(2);
				rsDto.setMessage("当前节点下有子节点 不能删除！");
				return rsDto;
			}
			int fa=bdKnowledgeCategoryEntityDao.selectByTypeId(knowledgeCategory);
			if(fa >= 1 ){
				rsDto.setStatus(3);
				rsDto.setMessage("该类别下有知识 不能删除！");
				return rsDto;
			}
			bdKnowledgeCategoryEntityDao.deleteByPrimaryKey(knowledgeCategory);
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		
		return rsDto;

	}

	
	/**
	 * 判断名称是否已经存在，  用于类型增加和修改前判断
	 * @param fwParent  父节点id
	 * @return
	 */
	public List<BdKnowledgeCategoryEntity> selectNameIsExist(String fwParent){
		List<BdKnowledgeCategoryEntity> categoryList=bdKnowledgeCategoryEntityDao.getCategoryParentName(fwParent);	
		return categoryList;
	}
	
	/**
	 * 新增类型
	 */
	public ResultDto insertCategory(BdKnowledgeCategoryEntityVo supplier) {
		ResultDto rsDto = new ResultDto();
		String id=MathUtil3.uuid32();
		supplier.setPk_knowledge_category(id);
		List<BdKnowledgeCategoryEntity> categoryList=selectNameIsExist(supplier.getFw_parent());
		if(categoryList != null && categoryList.size() > 0){
			for (int i = 0; i < categoryList.size(); i++) {
				if(categoryList.get(i).getKnowledge_category_name().equals(supplier.getKnowledge_category_name())){
					rsDto.setStatus(2);
					rsDto.setMessage("类别名称已存在！");
					return rsDto;
				}	
			}
		} 
		
		String knowledge_category_code = bdKnowledgeCategoryEntityDao.selectKnowledgeCategoryCode(supplier.getFw_parent().toString());
		if(knowledge_category_code == null){
			knowledge_category_code = "1";
		}
		Integer max = Integer.valueOf(knowledge_category_code);
		supplier.setKnowledge_category_code((++max).toString());
//		supplier.setFw_level(1);	//级次
		int a=  bdKnowledgeCategoryEntityDao.insertCategory(supplier);
		if(a>0 && !StringUtils.isEmpty(supplier.getPk_user())){
			
			BdCategoryRelationEntity relation = new  BdCategoryRelationEntity();
			relation.setPk_knowledge_category(id);  //类型id
			relation.setSealmark("Y");  //是否禁用    Y 启用
			String[] users = supplier.getPk_user().split(",");
			for(String str : users){
				relation.setPk_category_relation(IDFactory.getInstance().getID()); //主键id
				relation.setPk_user(str); 	// 用户id
				int b =bdKnowledgeCategoryEntityDao.insertRelation(relation);
			}
					
			rsDto.setStatus(0);
			rsDto.setMessage("添加成功！");
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("添加失败!");
		}

		return rsDto;
	}
	
	/**
	 * 修改类别
	 */
	public ResultDto updateByParamKey(BdKnowledgeCategoryEntityVo supplier) {
		ResultDto rsDto = new ResultDto();
		List<BdKnowledgeCategoryEntity> categoryList=selectNameIsExist(supplier.getFw_parent());
		if(categoryList != null && categoryList.size() > 0){
			for (int i = 0; i < categoryList.size(); i++) {
				if(categoryList.get(i).getKnowledge_category_name().equals(supplier.getKnowledge_category_name())){
					rsDto.setStatus(2);
					rsDto.setMessage("类别名称已存在！");
					return rsDto;
				}	
			}
		} 
		int a=  bdKnowledgeCategoryEntityDao.updateCategory(supplier);	
		if(a>0){
			BdCategoryRelationEntity relation = new  BdCategoryRelationEntity();
			relation.setPk_knowledge_category(supplier.getPk_knowledge_category());  //类型id
			relation.setSealmark(supplier.getSealmark());  							 //是否禁用    Y 启用
			relation.setPk_creator(supplier.getUpate_user());  					//当前登录用户id
			relation.setDtcreate(new Date());										//当前登录时间
			String pkuser=supplier.getPk_user();
			String [] str = pkuser.split(",");
			if(str != null && str.length > 0){
				int delCount=bdKnowledgeCategoryEntityDao.deleteByCategoryId(supplier.getPk_knowledge_category()); 
				if(delCount > 0){
					for (int i = 0; i < str.length; i++) {
						relation.setPk_category_relation(MathUtil3.uuid32()); 			//主键id
						relation.setPk_user(str[i]); 							 		// 用户id
						int b =bdKnowledgeCategoryEntityDao.insertRelation(relation);	
						System.out.println(b);
					}
				}	
			}
			rsDto.setStatus(0);
			rsDto.setMessage("修改成功！");
		}
		return rsDto;
	}
	
	
	public ResultDto queryKnoledgeCateForTree() {
		ResultDto rsDto = new ResultDto();
		List<BdKnowledgeCateTreePoJo> rsList = new ArrayList<BdKnowledgeCateTreePoJo>();
		//首先查询出来第一级
		List<BdKnowledgeCategoryEntity> listvo1 = bdKnowledgeCategoryEntityDao.selectByCondition(" (fw_parent is null or trim(fw_parent) ='') and sealmark='Y'");
		if(listvo1!=null && listvo1.size()>0){
			for(BdKnowledgeCategoryEntity vo1 : listvo1){
				BdKnowledgeCateTreePoJo po1 = new BdKnowledgeCateTreePoJo();
				po1.setPk_knowledge_category(vo1.getPk_knowledge_category());
				po1.setKnowledge_category_name(vo1.getKnowledge_category_name());
				po1.setKnowledge_category_code(vo1.getKnowledge_category_code());
				po1.setChildList(getAllChild(vo1));
				rsList.add(po1); 
			}
		}
		rsDto.setData(rsList);
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		return rsDto;
	}
	private List<BdKnowledgeCateTreePoJo> getAllChild(BdKnowledgeCategoryEntity vo){
		//查询子节点
		List<BdKnowledgeCateTreePoJo> childList = new ArrayList<BdKnowledgeCateTreePoJo>();
		List<BdKnowledgeCategoryEntity> listvo = bdKnowledgeCategoryEntityDao.selectByCondition(" fw_parent='"+vo.getPk_knowledge_category()+"'");
		if(listvo!=null && listvo.size()>0){
			for(BdKnowledgeCategoryEntity vo1 : listvo){
				BdKnowledgeCateTreePoJo po1 = new BdKnowledgeCateTreePoJo();
				po1.setPk_knowledge_category(vo1.getPk_knowledge_category());
				po1.setKnowledge_category_name(vo1.getKnowledge_category_name());
				po1.setKnowledge_category_code(vo1.getKnowledge_category_code());
				childList.add(po1);
			}
		}
		return childList;
	}

	public List<BdKnowledgeCategoryEntityVo> getAllKnowledgeCategoryRelation(String pk_knowledge_category) {
	
		return 	bdKnowledgeCategoryEntityDao.getAllKnowledgeCategoryRelation(pk_knowledge_category);
	}
}
